package utils;

import java.io.PrintStream;

public class Logger
{
    private static PrintStream out = System.out;
    private static PrintStream err = System.err;

    // close the stream while application exit
    static
    {
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            public void run()
            {
                out.close();
                err.close();
            }
        });
    }

    public static void logInfo(String info)
    {
        if (Util.isLogInfo())
            out.println(info);
    }

    public static void logInfo(String info, boolean mustLog)
    {
        if (mustLog)
            out.println(info);
        else
            logInfo(info);
    }

    public static void logException(Exception e)
    {
        if (Util.isLogError())
            e.printStackTrace(err);
    }

    public static void logException(Exception e, boolean mustLog)
    {
        if (mustLog)
            e.printStackTrace(err);
        else
            logException(e);
    }

    public static void logException(String e)
    {
        if (Util.isLogError())
            err.println(e);
    }

    public static void logException(String e, boolean mustLog)
    {
        if (mustLog)
            err.println(e);
        else
            logException(e);
    }

    public static PrintStream getErr()
    {
        return err;
    }

    public static void setErr(PrintStream err)
    {
        Logger.err = err;
    }

    public static PrintStream getOut()
    {
        return out;
    }

    public static void setOut(PrintStream out)
    {
        Logger.out = out;
    }
}
